<template>
  <div>
    <el-form ref="form" :model="form" label-width="100px" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择部门负责人">
          <el-option
            v-for="obj in employList"
            :key="obj.id"
            :label="obj.username"
            :value="obj.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="活动形式" prop="introduce">
        <el-input v-model="form.introduce" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="deptsSubmit">确认</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployessAPI, addDepartmentAPI, addDepartDetail, editDepartDetail } from '@/api/departments'
export default {
  name: 'DeptDialog',
  props: {
    // 父级部门ID
    parentId: {
      type: [Number, String],
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }

  },
  data() {
    // 验证编码
    var validCode = (rule, value, callback) => {
      // 需要将code组成一个数组
      let existCodeList = this.originList.map(item => item.code)

      // 先判断是否为编辑
      if (this.isEdit) {
        // 使用filter筛选parentId与id不相等的数组
        // if (this.form.code === value) {
        //   callback()
        // } else {
        //   existCodeList.includes(value) ? callback(new Error('部门编码' + value + '已经存在过')) : callback()
        // }
        existCodeList = this.originList.filter(item => item.id !== this.parentId).map(item => item.code)
      }
      // console.log(existCodeList)
      existCodeList.includes(value) ? callback(new Error('部门编码' + value + '已经存在过')) : callback()
    }
    // 验证同级部门不能重复
    var validName = (rule, value, callback) => {
      let existNameList = this.originList.filter(item => item.pid === this.parentId).map(item => item.name)
      // 判断是否编辑
      if (this.isEdit) {
        //  先获取本身
        const obj = this.originList.find(item => item.id === this.parentId)
        // 使用filter获取同级载排除本身再组成新的name数组
        existNameList = this.originList.filter(item => item.pid === obj.pid).filter(item => item.id !== this.parentId).map(item => item.name)
      }

      existNameList.includes(value) ? callback(new Error('部门名称' + value + '已经存在过')) : callback()
    }
    return {
      form: {
        code: '', // 部门编码
        introduce: '', // 介绍
        manager: '', // 负责人名称
        name: ''
      },
      employList: [],
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: validCode }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getEmployessAPI()
    this.addDepartDetail()
  },
  methods: {
    // 数组回显
    async addDepartDetail() {
      if (this.isEdit) {
        const res = await addDepartDetail(this.parentId)
        this.form = res.data
      }
    },
    // 获取员工简单列表
    async getEmployessAPI() {
      const res = await getEmployessAPI()
      // console.log('员工信息', res)
      this.employList = res.data
    },
    // 编辑部门
    async editDepartDetail() {
      const res = await editDepartDetail(this.form)
      this.$message.success(res.message)
      this.$emit('add-list')
    },
    // 添加部门
    async addDepartmentAPI() {
      const res = await addDepartmentAPI({ ...this.form, pid: this.parentId })
      this.$message.success(res.message)
      this.$emit('add-list')
    },
    // 提交按钮
    deptsSubmit() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        this.isEdit ? this.editDepartDetail() : this.addDepartmentAPI()
      })
    },
    // 取消按钮
    cancel() {
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select {
  width: 100%;
}
</style>
